package xu.ye.view;

import java.util.ArrayList;
import java.util.List;

import xu.ye.view.GroupSmsActivity.SendReceiver;

import com.agent.crm.db.DBInitScript;
import com.agent.crm.db.DatabaseManager;
import com.agent.crm.util.VerifyUtil;
import com.agent.crm.view.R;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TypeSmsActivity extends Activity{
    private EditText et_typeSmsBody;
    private Button btn_send;
    private Button btn_return;
    private String send_type_sms_action = "com.pocketdigi.type";
    private DatabaseManager manager = null;
    private String contactsIds;
    private Intent intent;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        intent = getIntent();
        contactsIds = intent.getStringExtra("contactsIds");
        
        manager = new DatabaseManager(this);
        setContentView(R.layout.type_sms);
        et_typeSmsBody = (EditText)findViewById(R.id.typeSmsBodyId);
        btn_send = (Button)findViewById(R.id.typeSmsSendId);
        btn_return = (Button)findViewById(R.id.typeSmsReturnId);
        //设置点击事件
        btn_send.setOnClickListener(btnOnclick);
        btn_return.setOnClickListener(btnOnclick);
    }
    
    OnClickListener btnOnclick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.typeSmsSendId:
                String body = et_typeSmsBody.getText().toString();
                if(TextUtils.isEmpty(body)){
                    Toast.makeText(TypeSmsActivity.this, "编辑栏不能为空", 
                            Toast.LENGTH_LONG).show();
                    return;
                }
                StringBuilder sqlStr = new StringBuilder();
                sqlStr.append("select mobile_phone ");
                sqlStr.append(" from " + DBInitScript.Table_ContactsInfo + " where contacts_id in(" + contactsIds + ")");
                SQLiteDatabase database = manager.getWritableDatabase();
                Cursor cursor = database.rawQuery(sqlStr.toString(), null);
                int count = cursor.getCount();
                if(count <= 0){
                    return;
                }
                List<String> mbPhoneList = new ArrayList<String>();
                
                cursor.moveToFirst();
                for(int i = 0; i < count; i++){
                    cursor.moveToPosition(i);
                    String mobilePhone = cursor.getString(0);
                    Boolean bool = VerifyUtil.isCellphone(mobilePhone);
                    if(bool){
                        mbPhoneList.add(mobilePhone);
                    }
                }
                SendReceiver receiver = new SendReceiver();
                IntentFilter filter = new IntentFilter();
                filter.addAction(send_type_sms_action);
                
                registerReceiver(receiver, filter);
                SmsManager smsMgr = SmsManager.getDefault();
                Intent smsIntent = new Intent(send_type_sms_action);
                PendingIntent pi = PendingIntent.getBroadcast(TypeSmsActivity.this, 0, smsIntent, 0);
                
                for(String phone : mbPhoneList){
                    smsMgr.sendTextMessage(phone, null, body, pi, null);
                }
                break;
            case R.id.typeSmsReturnId:
                finish();
                break;
            default:
                break;
            }
            
            
        }
    };
    
    
    class SendReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            int resultCode = getResultCode();
            if(resultCode==Activity.RESULT_OK){
                Toast.makeText(TypeSmsActivity.this, "发送成功", Toast.LENGTH_LONG).show();
                finish();
            }else{
                Toast.makeText(TypeSmsActivity.this, "发送失败", Toast.LENGTH_LONG).show();
            }
        }
        
    }
    
    
    
    
}
